// 机构员工(医生)
// 导入数据库查询方法
const { getOne, update, query } = require('../../utils/dbTools');
const { isValidator3 } = require('../../utils/util');
const { getUploadedUrl } = require('../../utils/upload');

const tableName = 'staff';
const businessTableName = 'business';

/**
 * 查询用户详情
 * 
 * */
const queryMemberInfo = async (id) => {
    const result = await getOne(tableName, { id })
    return result
}

// 查询当前医生列表
const doctorList = async (req, res) => {
    const output = {
        code: 200,
        msg: "操作成功"
    }
    const { bus_id, page, size } = req.query;
    var sql = `select s.*,b.title as bus_title from ${tableName} s inner join ${businessTableName} b on s.bus_id=b.id`;
    // 机构id
    if (bus_id) {
        sql += ` where bus_id=${bus_id}`;
    }
    if (page && size) {
        sql += ` limit ${(page - 1) * size},${size}`;
    }
    const result = await query(sql);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    result.forEach(async (item) => {
        if (item.tags) {
            item.tags = item.tags.split(',');
        }
    });
    output.data = result;
    res.send(output);
}



// 查询医生的详情
const doctorDetail = async (req, res) => {
    const output = {
        code: 200,
        msg: "操作成功"
    }
    const { id } = req.query;
    if (!id) {
        output.code = 500;
        output.msg = '参数错误:缺少id';
        return res.send(output);
    }
    const result = await getOne(tableName, { id });

    // var sql = `select s.*,b.title as bus_title from ${tableName} s
    // inner join ${businessTableName} b on s.bus_id=b.id where s.id=${id}`;
    // const result = await query(sql);
    if (!result) {
        output.code = 500;
        output.msg = '未查询到结果';
        return res.send(output);
    }
    if (result.bus_id) {
        const { title } = await getOne(businessTableName, { id: result.bus_id });
        result.bus_title = title;
    }

    if (result.tags) {
        result.tags = result.tags.split(',');
    }
    // 返回结果
    output.data = {
        ...result
    }
    delete output.data.password;
    res.send(output);

}

// 更新信息
const doctorUpdate = async (req, res) => {
    const output = {
        code: 200,
        msg: "操作成功"
    }
    const { id, phone } = req.body;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误:缺少id"
        return res.send(output);
    }
    // 如果提交了手机号码
    if (phone) {
        // 检查手机号码是否重复
        if (!await isValidator3(`select * from ${tableName} where phone='${phone}' and id<>${id} limit 1`)) {
            output.code = 500;
            output.msg = "手机号码已存在"
            return res.send(output);
        }
    }
    // 要更新的信息

    const payload = { ...req.body }
    // 获取上传完成的图片地址
    const { file } = req;
    if (file) {
        payload.avatar = getUploadedUrl(file);
    }
    delete payload.id
    await update(id, payload, tableName,);

    res.send(output);
}


module.exports = {
    doctorList,
    doctorDetail,
    doctorUpdate,
    queryMemberInfo
}